﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI;
using Doo.WebUI.SiteBuilder.PropertyEditor;

namespace Doo.WebUI
{
    public class SplitPanel : WidgetControl
    {
        #region CSS定义

        const string CssResourceName = "Doo.WebUI.WidgetControls.SplitPanel.css";

        /// <summary>
        /// 是否不使用嵌入式CSS资源
        /// </summary>
        public static bool NoEmbedCss { get; set; }

        public static string DefaultLeftCss { get; set; }

        public static string DefaultRightCss { get; set; }

        public static string DefaultCss { get; set; }

        static SplitPanel()
        {
            DefaultCss = "Doo-SplitPanel";
            DefaultLeftCss = "l";
            DefaultRightCss = "r";
        }

        #endregion

        private PlaceHolder leftholder = new PlaceHolder();
        private PlaceHolder rightholder = new PlaceHolder();

        [ContainerProperty]
        public ControlCollection Left { get { return leftholder.Controls; } }

        [ContainerProperty]
        public ControlCollection Right { get { return rightholder.Controls; } }

        [WidgetProperty("左侧面板样式类", typeof(StringEditor))]
        public string LeftCss { get; set; }

        [WidgetProperty("左侧面板宽度", typeof(StringEditor))]
        public Unit LeftWidth { get; set; }

        [WidgetProperty("右侧面板宽度", typeof(StringEditor))]
        public Unit RightWidth { get; set; }

        [WidgetProperty("右侧面板样式类", typeof(StringEditor))]
        public string RightCss { get; set; }

        [WidgetProperty("总样式类", typeof(StringEditor))]
        public string CssClass { get; set; }

        protected override void CreateChildControls()
        {
            this.Controls.Add(leftholder);
            this.Controls.Add(rightholder);
            base.CreateChildControls();
        }

        protected override void OnPreRender(EventArgs e)
        {
            if (!NoEmbedCss)
            {
                ResourceManger.RegisterCSS(this.GetType(), CssResourceName);
            }
            base.OnPreRender(e);
        }

        protected override void Render(HtmlTextWriter writer)
        {
            #region <div> root

            writer.AddAttribute(HtmlTextWriterAttribute.Class, CssClass ?? DefaultCss);
            writer.RenderBeginTag(HtmlTextWriterTag.Div);

            #region <div> left

            if (LeftWidth != null && !LeftWidth.IsEmpty)
            {
                writer.AddStyleAttribute(HtmlTextWriterStyle.Width, LeftWidth.ToString());
            }
            writer.AddAttribute(HtmlTextWriterAttribute.Class, LeftCss ?? DefaultLeftCss);
            writer.RenderBeginTag(HtmlTextWriterTag.Div);
            leftholder.RenderControl(writer);
            writer.RenderEndTag();

            #endregion

            #region <div> right

            if (RightWidth != null && !RightWidth.IsEmpty)
            {
                writer.AddStyleAttribute(HtmlTextWriterStyle.Width, RightWidth.ToString());
            }
            writer.AddAttribute(HtmlTextWriterAttribute.Class, RightCss ?? DefaultRightCss);
            writer.RenderBeginTag(HtmlTextWriterTag.Div);
            rightholder.RenderControl(writer);
            writer.RenderEndTag();

            #endregion

            writer.RenderEndTag();

            #endregion
        }
    }
}
